﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.page
{
    public partial class page_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string selType = this.sel_type.Value;

            SmartSpace.Core.Page page = new SmartSpace.Core.Page();
            page.Id = Guid.NewGuid();
            page.SiteId = SsSite.Current.Id;
            page.Code = _ctlEditName.Text;
            page.Caption = _ctlEditCaption.Text;

            if (selType == "virtualpage")
            {
                if (!string.IsNullOrEmpty(_ctlSelTemplateId.Value))
                {
                    page.Type = PageType.Virtual;
                    page.TemplateId = new Guid(_ctlSelTemplateId.Value);
                    page.Url = string.Format("~/{0}", page.Code);
                }
                else
                {
                    Logger.Error("web", "add page 失败，没有选择页面模板");
                    return;
                }
            }
            else if (selType == "nativepage")
            {
                page.Type = PageType.Native;
                page.Url = _ctlEditNativePage.Text;
            }

            ICoreService service = ServiceLocator.Get<ICoreService>();
            if (service.SavePage(page))
            {
                Response.Redirect("./page-list.aspx");
            }
        }
    }
}